<?php

namespace app\index\controller;

use think\Config;

class Cron extends Base
{
    public function check_vip()
    {
        $currtime = time();
        model('AdminUser')->where('vip', 1)->where('vipendtime', 'between', [0, $currtime])->update(['vip' => 0]);
    }

    public function send_coupons()
    {
        $userid = input('userid/d');
        $type = input('type/s');
        $couponsid = input('couponsid/d');
        $clientid = input('clientid/d');
        $send_cid = input('couponsid/s');
        $targetuserid = input('userids/s');
        $ownerid = input('ownerid/s');
        $tagid = input('tagid/s');
        switch ($type) {
            case 'vip':
                $this->send_vip_coupons($userid, $couponsid);
                break;
            case 'gouche':
                $this->send_gouche_coupons($userid, $clientid, $couponsid);
                break;
            case 'xubao':
                $this->send_xubao_coupons($userid, $clientid, $couponsid);
                break;
            case 'baoyang':
                $this->send_baoyang_coupons($userid, $clientid, $couponsid);
                break;
            case 'baoyang15':
                $this->send_baoyang_coupons_15($userid, $clientid);
                break;
            case 'send_again':
                $this->send_again_coupons($send_cid);
                break;
            case 'append_again':
                $this->append_coupons($send_cid, $targetuserid);
                break;
            case 'send_taguser_coupons':
                if ($tagid) {
                    $userids = model('UserTag')->where('tagid', $tagid)->column('userid');
                    $this->send_taguser_coupons($send_cid, $userids);
                }
                break;
            case 'give':
                $this->give_coupons($send_cid, $targetuserid, $ownerid);
                break;
            case 'append_coupons_vip':
                // $filternumber = model('FilterNumber')->column('mobile');
                // $userids = model('AdminUser')->where('vip', 1)->where('addtime', '<=', 1582992000)->where('phone', 'not in', $filternumber)->column('userid');
                // $this->append_coupons_vip($send_cid, $userids);
                break;
        }
    }

    // 购买VIP赠送优惠券
    protected function send_vip_coupons($userid, $couponsid = 0)
    {
        $where['status'] = 1;
        $where['auditstatus'] = 1;
        if ($couponsid) {
            $where['id'] = $couponsid;
        } else {
            $where['isbuyvip'] = 1;
        }
        $uinfo = model('AdminUser')->field('userid,username,vipendtime,dealerid')->where('userid', $userid)->find();
        $where['createrid'] = ['in', ['1', '2', $uinfo['dealerid']]];
        $list = model('Coupons')->where($where)->order('id desc')->select();
        if (empty($list)) {
            exit();
        }
        $lists = [];
        $currtime = time();
        foreach ($list as $v) {
            $userid_list = model('CouponsUser')->where('couponsid', $v['id'])->column('userid');
            if (in_array($uinfo['dealerid'], $userid_list)) {
                continue;
            }
            if ($v['opentime'] > $currtime || $v['closetime'] < $currtime) {
                continue;
            }
            if ($v['id'] == 4) {
                $count = model('MemberCoupons')->where('userid', $userid)->where('couponsid', $v['id'])->count();
                if ($count) {
                    continue;
                }
            }
            $endtime = $v['endtime'];
            if ($v['isrelyvip']) {
                $endtime = $uinfo['vipendtime'];
            } elseif ($v['validdays']) {
                $validdays = $v['validdays'];
                $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
            }
            $isactivate = '1';
            if ($v['id'] == 4) {
                $isactivate = '0';
            }
            $lists[] = [
                'userid' => $userid,
                'couponsid' => $v['id'],
                'dealerid' => $uinfo['dealerid'],
                'type' => $v['type'],
                'name' => $v['name'],
                // 'image' => $v['image'],
                'couponsno' => $this->get_couponsno('V'),
                'distype' => $v['distype'],
                'price' => $v['price'],
                'isgive' => $v['isgive'],
                'isbuyvip' => $v['isbuyvip'],
                'usemore' => $v['usemore'],
                'starttime' => $v['starttime'] > time() ? $v['starttime'] : time(),
                'endtime' => $endtime,
                'isactivate' => $isactivate,
                // 'activatetime' => time() + 120,
                'activatetime' => strtotime(date('Y-m-d', strtotime('+1 day')) . ' 23:59:59'),
                'source' => '0',
                'desc' => $v['desc'],
                'tip' => $v['tip'],
            ];
        }
        model('MemberCoupons')->saveAll($lists);
    }

    // 为历史会员补发优惠券
    protected function send_again_coupons($c_id = '')
    {
        if (!$c_id) {
            exit();
        }
        $temp_file = ROOT_PATH . "sijia_bufa_lock.lock";
        if (file_exists($temp_file)) {
            exit();
        }
        file_put_contents($temp_file, "lock");
        // 首先获取优惠券详情
        $c_detail = model('Coupons')->where('id', $c_id)->find();
        // 需要屏蔽的经销商
        $dealerids = model('CouponsUser')->where('couponsid', 'in', $c_id)->column('userid');
        // 需要补发的会员列表
        $userids1 = model('MemberCoupons')->where('dealerid', 'not in', $dealerids)->group('userid')->column('userid');
        $userids2 = model('MemberCoupons')->where('couponsid', $c_id)->where('dealerid', 'not in', $dealerids)->group('userid')->column('userid');
        $useridlist = array_diff($userids1, $userids2);
        if (!$useridlist) {
            exit();
        }
        $limit = 100; // 每次处理100条数据
        $total = count($useridlist); // 总数据量
        $count = ceil($total / $limit);
        for ($i = 0; $i < $count; $i++) {
            $offset = $i * $limit;
            $userlist = array_slice($useridlist, $offset, $limit);
            $lists = [];
            foreach ($userlist as $uid) {
                $uinfo = model('AdminUser')->where('userid', $uid)->field('vipendtime, dealerid')->find();
                if (!$uinfo) {
                    continue;
                }
                $endtime = $c_detail['endtime'];
                if ($c_detail['isrelyvip']) {
                    $endtime = $uinfo['vipendtime'];
                } elseif ($c_detail['validdays']) {
                    $validdays = $c_detail['validdays'];
                    $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
                }
                $isactivate = '1';
                if ($c_detail['id'] == 4 || $c_detail['id'] == 11) {
                    $isactivate = '0';
                }
                $lists[] = [
                    'userid' => $uid,
                    'couponsid' => $c_id,
                    'dealerid' => $uinfo['dealerid'],
                    'type' => $c_detail['type'],
                    'name' => $c_detail['name'],
                    // 'image' => $c_detail['image'],
                    'couponsno' => $this->get_couponsno('V'),
                    'distype' => $c_detail['distype'],
                    'price' => $c_detail['price'],
                    'isgive' => $c_detail['isgive'],
                    'isbuyvip' => $c_detail['isbuyvip'],
                    'usemore' => $c_detail['usemore'],
                    'starttime' => $c_detail['starttime'] > time() ? $c_detail['starttime'] : time(),
                    'endtime' => $endtime,
                    'isactivate' => $isactivate,
                    'source' => '1',
                    'desc' => $c_detail['desc'],
                    'tip' => $c_detail['tip'],
                ];
            }
            model('MemberCoupons')->saveAll($lists);
        }
        if (file_exists($temp_file)) {
            unlink($temp_file);
        }
    }

    // 为某个会员补发优惠券
    protected function append_coupons($c_id = '', $targetuserid)
    {
        if (!$c_id) {
            exit();
        }
        // 首先获取优惠券详情
        $c_detail = model('Coupons')->where('id', $c_id)->find();
        // 需要补发的会员
        $uinfo = model('AdminUser')->where('userid', $targetuserid)->field('vipendtime, dealerid')->find();
        if (!$uinfo) {
            return;
        }
        $endtime = $c_detail['endtime'];
        if ($c_detail['isrelyvip']) {
            $endtime = $uinfo['vipendtime'];
        } elseif ($c_detail['validdays']) {
            $validdays = $c_detail['validdays'];
            $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
        }
        $isactivate = '1';
        if ($c_detail['id'] == 4 || $c_detail['id'] == 11) {
            $isactivate = '0';
        }
        $lists[] = [
            'userid' => $targetuserid,
            'couponsid' => $c_id,
            'dealerid' => $uinfo['dealerid'],
            'type' => $c_detail['type'],
            'name' => $c_detail['name'],
            // 'image' => $c_detail['image'],
            'couponsno' => $this->get_couponsno('V'),
            'distype' => $c_detail['distype'],
            'price' => $c_detail['price'],
            'isgive' => $c_detail['isgive'],
            'isbuyvip' => $c_detail['isbuyvip'],
            'usemore' => $c_detail['usemore'],
            'starttime' => $c_detail['starttime'] > time() ? $c_detail['starttime'] : time(),
            'endtime' => $endtime,
            'isactivate' => $isactivate,
            'source' => '1',
            'desc' => $c_detail['desc'],
            'tip' => $c_detail['tip'],
        ];
        model('MemberCoupons')->saveAll($lists);
    }

    // 赠送优惠券
    protected function give_coupons($c_id = '', $targetuserid, $ownerid = '')
    {
        if (!$c_id) {
            exit();
        }
        // 首先获取优惠券详情
        $c_detail = model('Coupons')->where('id', $c_id)->find();
        // 需要补发的会员
        $uinfo = model('AdminUser')->where('userid', $targetuserid)->field('vipendtime, dealerid')->find();
        if (!$uinfo) {
            return;
        }
        $endtime = $c_detail['endtime'];
        if ($c_detail['isrelyvip']) {
            $endtime = $uinfo['vipendtime'];
        } elseif ($c_detail['validdays']) {
            $validdays = $c_detail['validdays'];
            $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
        }
        $isactivate = '1';
        if ($c_detail['id'] == 11) {
            $isactivate = '0';
        }
        $status = '0';
        $count = model('MemberCoupons')->where('ownerid', $ownerid)->where('isactivate', 1)->count();
        if ($count) {
            $status = '2';
        }
        $lists[] = [
            'userid' => $targetuserid,
            'couponsid' => $c_id,
            'dealerid' => $uinfo['dealerid'],
            'type' => $c_detail['type'],
            'name' => $c_detail['name'],
            // 'image' => $c_detail['image'],
            'couponsno' => $this->get_couponsno('V'),
            'distype' => $c_detail['distype'],
            'price' => $c_detail['price'],
            'isgive' => $c_detail['isgive'],
            'isbuyvip' => $c_detail['isbuyvip'],
            'usemore' => $c_detail['usemore'],
            'starttime' => $c_detail['starttime'] > time() ? $c_detail['starttime'] : time(),
            'endtime' => $endtime,
            'isactivate' => $isactivate,
            'source' => '1',
            'status' => $status,
            'ownerid' => $ownerid,
            'desc' => $c_detail['desc'],
            'tip' => $c_detail['tip'],
        ];
        model('MemberCoupons')->saveAll($lists);
    }

    // 为标签会员补发优惠券
    protected function send_taguser_coupons($c_id = '', $targettagids)
    {
        if (!$c_id) {
            exit();
        }
        // $temp_file = ROOT_PATH . "sijia_bufa_lock.lock";
        // if (file_exists($temp_file)) {
        //     exit();
        // }
        // file_put_contents($temp_file, "lock");
        // 首先获取优惠券详情
        $c_detail = model('Coupons')->where('id', $c_id)->find();
        // // 需要屏蔽的经销商
        // $dealerids = model('CouponsUser')->where('couponsid', 'in', $c_id)->column('userid');
        // 需要补发的会员列表
        // $userids1 = model('MemberCoupons')->where('dealerid', 'not in', $dealerids)->group('userid')->column('userid');
        // $userids2 = model('MemberCoupons')->where('couponsid', $c_id)->where('dealerid', 'not in', $dealerids)->group('userid')->column('userid');
        // $useridlist = array_diff($userids1,  $userids2);
        if (!$targettagids) {
            exit();
        }
        $limit = 100; // 每次处理100条数据
        $total = count($targettagids); // 总数据量
        $count = ceil($total / $limit);
        for ($i = 0; $i < $count; $i++) {
            $offset = $i * $limit;
            $userlist = array_slice($targettagids, $offset, $limit);
            $lists = [];
            foreach ($userlist as $uid) {
                $uinfo = model('AdminUser')->where('userid', $uid)->field('vipendtime, dealerid')->find();
                if (!$uinfo) {
                    continue;
                }
                $endtime = $c_detail['endtime'];
                if ($c_detail['isrelyvip']) {
                    $endtime = $uinfo['vipendtime'];
                } elseif ($c_detail['validdays']) {
                    $validdays = $c_detail['validdays'];
                    $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
                }
                $isactivate = '1';
                if ($c_detail['id'] == 4) {
                    $isactivate = '0';
                }
                $lists[] = [
                    'userid' => $uid,
                    'couponsid' => $c_id,
                    'dealerid' => $uinfo['dealerid'],
                    'type' => $c_detail['type'],
                    'name' => $c_detail['name'],
                    // 'image' => $c_detail['image'],
                    'couponsno' => $this->get_couponsno('V'),
                    'distype' => $c_detail['distype'],
                    'price' => $c_detail['price'],
                    'isgive' => $c_detail['isgive'],
                    'isbuyvip' => $c_detail['isbuyvip'],
                    'usemore' => $c_detail['usemore'],
                    'starttime' => $c_detail['starttime'] > time() ? $c_detail['starttime'] : time(),
                    'endtime' => $endtime,
                    'isactivate' => $isactivate,
                    'source' => '1',
                    'desc' => $c_detail['desc'],
                    'tip' => $c_detail['tip'],
                ];
            }
            model('MemberCoupons')->saveAll($lists);
        }
        // if (file_exists($temp_file)) {
        //     unlink($temp_file);
        // }
    }

    // 购车成交之后发放 购车奖
    protected function send_gouche_coupons($userid, $clientid = 0, $couponsid = 0)
    {
        if (!$clientid) {
            exit();
        }
        $where = ['status' => 1, 'isbuyvip' => 0];
        if ($couponsid) {
            $where['id'] = $couponsid;
        } else {
            $where['type'] = 'gouche';
        }
        $count = model('MemberCoupons')->where('userid', $userid)->where('type', 'gouche')->count();
        if (!$count) {
            $list = model('Coupons')->where($where)->order('id desc')->select();
            if (!empty($list)) {
                $lists = [];
                $uinfo = model('AdminUser')->field('userid,username,vipendtime,dealerid')->where('userid', $userid)->find();
                foreach ($list as $v) {
                    $endtime = $v['endtime'];
                    if ($v['isrelyvip']) {
                        $endtime = $uinfo['vipendtime'];
                    } elseif ($v['validdays']) {
                        $validdays = $v['validdays'];
                        $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
                    }
                    $lists[] = [
                        'userid' => $userid,
                        'couponsid' => $v['id'],
                        'dealerid' => $uinfo['dealerid'],
                        'type' => $v['type'],
                        'name' => $v['name'],
                        // 'image' => $v['image'],
                        'couponsno' => $this->get_couponsno('G'),
                        'distype' => $v['distype'],
                        'price' => $v['price'],
                        'isgive' => $v['isgive'],
                        'isbuyvip' => $v['isbuyvip'],
                        'usemore' => $v['usemore'],
                        'starttime' => $v['starttime'] > time() ? $v['starttime'] : time(),
                        'endtime' => $endtime,
                        'source' => '2',
                        'desc' => $v['desc'],
                        'tip' => $v['tip'],
                    ];
                }
                model('MemberCoupons')->saveAll($lists);
            }
        }
        $count = model('MemberBouns')->where('userid', $userid)->where('clientid', $clientid)->count();
        if (!$count) {
            $binfo = [
                'userid' => $userid,
                'clientid' => $clientid,
                'price' => config('BUYCAR_BOUNS'),
                'status' => 1,
                'sendtime' => time(),
            ];
            model('MemberBouns')->save_data($binfo);
        }
    }

    // 续保线索成功之后 发放续保券
    protected function send_xubao_coupons($userid, $clientid = 0, $couponsid = 0)
    {
        if (!$clientid) {
            exit();
        }

        $count = model('MemberCoupons')->where('userid', $userid)->where('type', 'xubao')->where('clientid', $clientid)->count();
        if ($count) {
            exit();
        }
        $where = ['status' => 1, 'isbuyvip' => 0];
        if ($couponsid) {
            $where['id'] = $couponsid;
        } else {
            $where['type'] = 'xubao';
        }
        $list = model('Coupons')->where($where)->order('id desc')->select();
        if (!empty($list)) {
            $lists = [];
            $uinfo = model('AdminUser')->field('userid,username,vipendtime,dealerid')->where('userid', $userid)->find();
            foreach ($list as $v) {
                $endtime = $v['endtime'];
                if ($v['isrelyvip']) {
                    $endtime = $uinfo['vipendtime'];
                } elseif ($v['validdays']) {
                    $validdays = $v['validdays'];
                    $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
                }
                $lists[] = [
                    'userid' => $userid,
                    'clientid' => $clientid,
                    'couponsid' => $v['id'],
                    'dealerid' => $uinfo['dealerid'],
                    'type' => $v['type'],
                    'name' => $v['name'],
                    // 'image' => $v['image'],
                    'couponsno' => $this->get_couponsno('X'),
                    'distype' => $v['distype'],
                    'price' => $v['price'],
                    'isgive' => $v['isgive'],
                    'isbuyvip' => $v['isbuyvip'],
                    'usemore' => $v['usemore'],
                    'starttime' => $v['starttime'] > time() ? $v['starttime'] : time(),
                    'endtime' => $endtime,
                    'source' => '2',
                    'desc' => $v['desc'],
                    'tip' => $v['tip'],
                ];
            }
            model('MemberCoupons')->saveAll($lists);
        }
    }

    // 保养线索成功之后 发放保养券
    protected function send_baoyang_coupons($userid, $clientid = 0, $couponsid = 0)
    {
        if (!$clientid) {
            exit();
        }
        $price = model('Client')->where('id', $clientid)->value('price');
        if ($price == null || $price == '') {
            $price = '0';
        }
        $price = round($price * 0.1, 2);
        $count = model('MemberCoupons')->where('userid', $userid)->where('type', 'baoyang')->where('clientid', $clientid)->count();
        if ($count) {
            exit();
        }
        $where = ['status' => 1, 'isbuyvip' => 0];
        if ($couponsid) {
            $where['id'] = $couponsid;
        } else {
            $where['type'] = 'baoyang';
        }
        $list = model('Coupons')->where($where)->order('id desc')->select();
        if (!empty($list)) {
            $lists = [];
            $uinfo = model('AdminUser')->field('userid,username,vipendtime,dealerid')->where('userid', $userid)->find();
            foreach ($list as $v) {
                $endtime = $v['endtime'];
                if ($v['isrelyvip']) {
                    $endtime = $uinfo['vipendtime'];
                } elseif ($v['validdays']) {
                    $validdays = $v['validdays'];
                    $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
                }
                // $v['activatedate'] = strtotime('+7 day');
                $lists[] = [
                    'userid' => $userid,
                    'clientid' => $clientid,
                    'couponsid' => $v['id'],
                    'dealerid' => $uinfo['dealerid'],
                    'type' => $v['type'],
                    'name' => $v['name'],
                    // 'image' => $v['image'],
                    'couponsno' => $this->get_couponsno('B'),
                    'distype' => $v['distype'],
                    'price' => $v['price'],
                    'isgive' => $v['isgive'],
                    'isbuyvip' => $v['isbuyvip'],
                    'usemore' => $v['usemore'],
                    'starttime' => $v['starttime'] > time() ? $v['starttime'] : time(),
                    'endtime' => $endtime,
                    'price2' => $price,
                    'source' => '2',
                    'desc' => $v['desc'],
                    'tip' => $v['tip'],
                ];
            }
            model('MemberCoupons')->allowField(true)->saveAll($lists);
        }
    }

    // 续费会员保养线索成功之后 发放15%的保养券
    protected function send_baoyang_coupons_15($userid, $clientid = 0)
    {
        if (!$clientid) {
            exit();
        }
        $price = model('Client')->where('id', $clientid)->value('price');
        if ($price == null || $price == '') {
            $price = '0';
        }
        $price = round($price * 0.15, 2);
        $count = model('MemberCoupons')->where('userid', $userid)->where('couponsid', 12)->count();
        if ($count) {
            exit();
        }
//        $where = ['status' => 1, 'isbuyvip' => 0];
//        if ($couponsid) {
//            $where['id'] = $couponsid;
//        } else {
//            $where['type'] = 'baoyang';
//        }
        $list = model('Coupons')->where('id', 12)->select();
        if (!empty($list)) {
            $lists = [];
            $uinfo = model('AdminUser')->field('userid,username,vipendtime,dealerid')->where('userid', $userid)->find();
            foreach ($list as $v) {
                $endtime = $v['endtime'];
                if ($v['isrelyvip']) {
                    $endtime = $uinfo['vipendtime'];
                } elseif ($v['validdays']) {
                    $validdays = $v['validdays'];
                    $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
                }
                // $v['activatedate'] = strtotime('+7 day');
                $lists[] = [
                    'userid' => $userid,
                    'clientid' => $clientid,
                    'couponsid' => $v['id'],
                    'dealerid' => $uinfo['dealerid'],
                    'type' => $v['type'],
                    'name' => $v['name'],
                    // 'image' => $v['image'],
                    'couponsno' => $this->get_couponsno('B'),
                    'distype' => $v['distype'],
                    'price' => $v['price'],
                    'isgive' => $v['isgive'],
                    'isbuyvip' => $v['isbuyvip'],
                    'usemore' => $v['usemore'],
                    'starttime' => $v['starttime'] > time() ? $v['starttime'] : time(),
                    'endtime' => $endtime,
                    'price2' => $price,
                    'source' => '2',
                    'desc' => $v['desc'],
                    'tip' => $v['tip'],
                ];
            }
            model('MemberCoupons')->allowField(true)->saveAll($lists);
        }
    }

    protected function get_couponsno($str = '')
    {
        $no = $str . strtoupper(create_randomstr(8));
        $count = model('MemberCoupons')->where('couponsno', $no)->count();
        if ($count) {
            return $this->get_couponsno($str);
        }
        return $no;
    }

    // //计算中签率，更新热度
    // public function refresh_hot(){
    // 	$list = model("Haoling")->where("status = 1")->field("nums,shengous,id")->order("id")->select();
    //     foreach($list as $v){
    //         $hot = round($v['nums'] / $v['shengous'] * 100) / 100;
    //         if($hot > 0.5){
    //             $redu = 1;
    //         }
    //         else if($hot > 0.2 && $hot <= 0.5){
    //             $redu = 2;
    //         }
    //         else if($hot > 0.1 && $hot <= 0.2){
    //             $redu = 3;
    //         }
    //         else if($hot <= 0.1){
    //             $redu = 4;
    //         }
    //         model("Haoling")->where("id",$v['id'])->setField("redu",$redu);
    //     }
    // }
    // //根据中签率，计算购买成功的用户
    // public function set_user_haoling_success(){
    //     //设置号令开始
    //     $nowtime = SYS_TIME;
    //     $list = db("HaolingQishu")->where("starttime <= $nowtime and endtime > $nowtime")->order("id")->select();
    //     foreach($list as $v){
    //         db("Haoling")->where("status",0)->where("qishu",$v['id'])->setField("status",1); //标记开始
    //     }

    //     //重新计算中签率
    //     $hotarr = [];
    //     $list = model("Haoling")->where("status = 1")->field("nums,shengous,id")->order("id")->select();
    //     foreach($list as $v){
    //         $hot = round($v['nums'] / $v['shengous'] * 100) / 100;
    //         $hotarr[$v['id']] = $hot;
    //     }

    //     $list = model("HaolingBuylog")->where("status = 0")->order("id")->select();
    //     foreach($list as $v){
    //         //floor
    //         if($hotarr[$v['haolingid']] > 1){
    //             $buynums = $v['nums'];
    //         }
    //         else{
    //             $buynums = floor($v['nums'] * $hotarr[$v['haolingid']]);
    //         }
    //         //退换多余的款
    //         $lastprice = $v['price'] * ($v['nums'] - $buynums);
    //         // //退款
    //         model("MemberAmountLog")->add($v['userid'],1,5,$lastprice,"申购号令退款");
    //         // //加入我的号令表
    //         $v['nums'] = $buynums;
    //         model("MemberHaoling")->save_data($v);

    //         model("Message")->add_msg($v['userid'],"申购的号令处理完成了");
    //         model("Dynamic")->add_msg($v['userid'],"申购的号令处理完成了");

    //         db("HaolingBuylog")->where("id",$v['id'])->update(['status'=>1,'buynums'=>$buynums]);
    //         db("Haoling")->where("id",$v['haolingid'])->setField("status",2); //申购结束
    //     }
    // }
    // public function sale_daling_money(){
    //     $temp_file = ROOT_PATH."runtime/temp/price.lock";
    //     if(file_exists($temp_file)){
    //         exit();
    //     }
    //     file_put_contents($temp_file, "lock");
    //     while(true){
    //         $qinfo = db("OrderQueue")->where("errors = 0")->order("id asc")->find(); //循环队列
    //         if(empty($qinfo)) break;
    //         //立马删除
    //         db("OrderQueue")->where(array('id'=>$qinfo['id']))->delete();

    //         $lastnums = model("Trade","service")->match_trade($qinfo['orderid']);

    //         if($lastnums === false){
    //             continue;
    //         }

    //         if($lastnums > 0){ //如果没买够，加入队列，进行下一次交易
    //             $queue_data = array(
    //                 'orderid' => $qinfo['orderid'],
    //                 'ordertime' => $qinfo['ordertime'],
    //                 'errors' => 1,
    //             );
    //             db("OrderQueue")->insert($queue_data);
    //         }
    //     }
    //     unlink($temp_file);
    // }
    // public function clier_sql(){
    //     $list = model('Member')->order('id desc')->select();
    //     foreach ($list as $v) {
    //         model('Member')->where('id', $v['id'])->update(['amount'=>0.00,'margin'=>0.00,'dis_amount'=>0.00,'dis_margin'=>0.00,'bmoney'=>0.00,'zans'=>0,'favs'=>0,'jifen'=>100,'signs'=>0,'pick_totalprice'=>0,'order_totalprice'=>0,'loginnums'=>0,'sucnums'=>0,'sucorders'=>0]);
    //     }
    //     print_r(model('Member')->getError());
    // }
    // public function exec_data(){
    //     exec_php("index/cron/set_thumb_path");
    // }
    // //12519  12520 尚未处理
    // public function down_put_image(){
    //     $page = 996;
    //     while ($page < 5000) {
    //         $list = model('Car')->page($page, 10)->order('id asc')->select();
    //         foreach ($list as $v) {
    //             if (!@getimagesize($v['thumb'])) {
    //                 continue;
    //             }
    //             $pid = model('Cartype')->where('id', $v['pid'])->value('pid');
    //             $cartypeinfo = model('Cartype')->where('id', $pid)->find();
    //             $file = file_get_contents($v['thumb']);
    //             trace($file);
    //             $path = 'upload/car/'.($cartypeinfo['pid'] ? $cartypeinfo['pid'] : $cartypeinfo['id']);
    //             if (!is_dir($path)) {
    //                 mkdir($path);
    //             }
    //             $path .= '/'.md5(date('YmdHis', SYS_TIME).rand(100000, 999999)).'.png';
    //             // trace($path);
    //             $ids = file_put_contents(ROOT_PATH.$path, $file);
    //             model('Car')->where('id', $v['id'])->setField('thumb', $path);
    //         }
    //         $page++;
    //         sleep(0.2);
    //     }
    //     echo 'OK';
    // }
    // public function set_thumb_path(){
    //     $page = 1500;
    //     while ($page < 5000) {
    //         $list = model('Car')->page($page, 20)->order('id asc')->select();
    //         if (empty($list)) {
    //             break;
    //         }
    //         foreach ($list as $v) {
    //             // if(strpos($v['thumb'],"http://") === false){
    //             //     if (substr($v['thumb'], 0, 1) != '/') {
    //             //         model('Car')->where('id', $v['id'])->setField('thumb', '/'.$v['thumb']);
    //             //     }
    //             // }
    //             if (strpos($v['thumb'], 'http://api.jisuapi.com' !== false)) {
    //                 continue;
    //             }

    //             $thumb = str_replace('http://http:/upload/', '/upload/', $v['thumb']);
    //             model('Car')->where('id', $v['id'])->setField('thumb', $thumb);
    //         }
    //         $page++;
    //         sleep(0.2);
    //     }
    //     echo 'OK';
    // }
    //统计注册人数
    public function stat_member()
    {
        $page = 1;
        while ($page < 100) {
            $list = model('AdminUser')->where('userid', 'gt', 1)->field('userid, addtime')->order('addtime asc')->page($page, 10)->select();
            foreach ($list as $v) {
                model('MemberStatistics')->add_log(1, 1, $v['addtime']);
            }
            $page++;
        }
        echo "OK";
    }


    // // 为2020.3.1之前的荣耀会员发放“2000元购车券”
    // protected function append_coupons_vip($c_id = '', $userids)
    // {
    //     if (!$c_id) {
    //         exit();
    //     }
    //     // 首先获取优惠券详情
    //     $c_detail = model('Coupons')->where('id', $c_id)->find();
    //     if (!$userids) {
    //         exit();
    //     }
    //     $limit = 100; // 每次处理100条数据
    //     $total = count($userids); // 总数据量
    //     $count = ceil($total / $limit);
    //     for ($i = 0; $i < $count; $i++) {
    //         $offset = $i * $limit;
    //         $userlist = array_slice($userids, $offset, $limit);
    //         $lists = [];
    //         foreach ($userlist as $uid) {
    //             $uinfo = model('AdminUser')->where('userid', $uid)->field('vipendtime, dealerid')->find();
    //             if (!$uinfo) {
    //                 continue;
    //             }
    //             $endtime = $c_detail['endtime'];
    //             if ($c_detail['isrelyvip']) {
    //                 $endtime = $uinfo['vipendtime'];
    //             } elseif ($c_detail['validdays']) {
    //                 $validdays = $c_detail['validdays'];
    //                 $endtime = strtotime(date('Y-m-d', time() + $validdays * 86400) . ' 23:59:59');
    //             }
    //             $lists[] = [
    //                 'userid' => $uid,
    //                 'couponsid' => $c_id,
    //                 'dealerid' => $uinfo['dealerid'],
    //                 'type' => $c_detail['type'],
    //                 'name' => $c_detail['name'],
    //                 // 'image' => $c_detail['image'],
    //                 'couponsno' => $this->get_couponsno('V'),
    //                 'distype' => $c_detail['distype'],
    //                 'price' => $c_detail['price'],
    //                 'isgive' => $c_detail['isgive'],
    //                 'isbuyvip' => $c_detail['isbuyvip'],
    //                 'usemore' => $c_detail['usemore'],
    //                 'starttime' => $c_detail['starttime'] > time() ? $c_detail['starttime'] : time(),
    //                 'endtime' => $endtime,
    //                 'isactivate' => '0',
    //                 'source' => '1',
    //                 'desc' => $c_detail['desc'],
    //                 'tip' => $c_detail['tip'],
    //             ];
    //         }
    //         model('MemberCoupons')->saveAll($lists);
    //     }
    // }
}
